import sys_user from "../model/sys_user";
import response from "../util/response";

/*
 * @Author: fishroom
 * @Date: 2022-09-28 16:18:10
 * @LastEditTime: 2022-10-19 10:20:16
 * @FilePath: \koa\app\server\UserServer.ts
 */
class UserServer {
  async getUser() {
    return await sys_user.findAll();
  }
  async addUser(newUser: any) {
    try {
      let selectuser = await sys_user.findOne({
        where: {
          username: newUser.username,
        },
      });
      if (!selectuser) {
        let data = await sys_user.create(newUser);
        return response.success(data, "添加成功");
      }
      return response.error("失败，用户名重复");
    } catch (error) {
      return response.error("失败", { error });
    }
  }
  async updataUserById(AUser: any) {
    let user_id = AUser.user_id;
    delete AUser.user_id;
    try {
      await sys_user.update(
        { ...AUser },
        {
          where: {
            user_id,
          },
        }
      );
      return response.success({}, "成功");
    } catch (error) {
      return response.error("添加失败", { error });
    }
  }
  async LoginAdmin(AUser: any) {
    let { username, password } = AUser;
    try {
      let admin = await sys_user.findOne({
        where: {
          username,
          password,
        },
      });
      return { admin, error: null };
    } catch (error) {
      return { admin: null, error };
    }
  }
}
export default new UserServer();
